//
//  MyOrderTableViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/11.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class MyOrderTableViewController: UITableViewController,LgCallBack,MyOrderTableViewCellBtnClickDelegate{
    var moList = [MyOrder]()
    var pageNo = 1
    let header = MJRefreshNormalHeader()
    let footer = MJRefreshAutoNormalFooter()
    var selectNum = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        header.setRefreshingTarget(self, refreshingAction:#selector(self.headerRefresh))
        self.tableView.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction:#selector(self.footerRefresh))
        self.tableView.mj_footer = footer
        
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return moList.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath)
        as! MyOrderTableViewCell
        cell.delegate = self
        cell.selectNum = indexPath.row
        cell.mo = moList[indexPath.row]

        return cell
    }
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(1)
        moList.removeAll()
        //重现生成数据
        pageNo = 1
        getData()
        
    }
    
    func footerRefresh(){
        print("提拉加载")
        sleep(1)
        pageNo += 1
        getData()
    }
    
    func getData(){
        asyn_req_post(method:  M_MyOrder, cb: self, p: ["accountId" : user.accountId as AnyObject,"pageNo":pageNo as AnyObject,"pageSize":10 as AnyObject])
    }

    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            moList += parseMyOrderList(all:j["MobileBody"].arrayValue)
            header.endRefreshing()
            footer.endRefreshing()
        }
        tableView.reloadData()
    }
    
    func payBtnClick(indexpath: Int) {
        print("去支付\(indexpath)")
        selectNum = indexpath
        self.performSegue(withIdentifier: "toPay", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toPay"{
            let dest = segue.destination as! MakeSureViewController
            dest.orderId = moList[selectNum].id
            dest.perOrder.totalPrice = moList[selectNum].payAmount
            dest.payFor = "other";
        }
    }
}
